<?php
require_once(DP_BASE_DIR."/modules/masterdata/mdtype.class.php");
$type_id = intval( dPgetParam( $_GET, 'type_id', 0 ) );

// check permissions for this record
/*
$perms =& $AppUI->acl();
if (! ($canEdit = $perms->checkModuleItem( 'currencys', 'edit', $currency_id )) ) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
*/

// load the record data
$msg = '';
$row = new CMdType();

$canDelete = $row->canDelete( $msg, $type_id );
if($msg == $AppUI->_('typeDeleteUserError', UI_OUTPUT_JS)) {
	$userDeleteProtect=true;
}

if ($view) {
	$view = ' readonly="true"';
}
else {
	unset($view);
}

if (!$row->load( $type_id ) && $type_id > 0) {
	$AppUI->setMsg( 'Type' );
	$AppUI->setMsg( 'invalidID', UI_MSG_ERROR, true );
	$AppUI->redirect();
}

$itemScreens = array();
if ($type_id > 0) {
	$q = new DBQuery();
	$q->addTable('item_screens');
	$q->addQuery('item_screen');
	$q->addWhere("item_type=$type_id");
	$results = $q->loadList();
	$itemScreens = array();
	foreach($results as $i) {
		$itemScreens[] = $i['item_screen'];
	}
}

$q = new DBQuery();
$q->addTable('md_type');
$q->addQuery('code');
if (isset($row->code)) {
	$q->addWhere("type_id != $row->type_id");
}
$quoteCodeList = $q->loadColumn();
$qCL = '["' . implode('", "', $quoteCodeList) . '"]';

if (!$view) {
?>
<form name="MdTypeForm" action="?m=masterdata" method="post">
<input type="hidden" name="dosql" value="mdtype_do_aed" />
<input type="hidden" name="type_id" value="<?=$type_id?>" />
<?}?>
<table border="0" cellpadding="8" cellspacing="0" width="400px">
<tr>
<td colspan="2" bgcolor="#E0E0E0"><b><?=$AppUI->_('Type Information')?></td>
</tr>
<tr>
	<td colspan="2">
		<table border="0" cellpadding="0" cellspacing="4" width="100%" align="center">
		<tr>
			<td align="right"><?=$AppUI->_('Type')?></td>
			<td>
				<? echo arraySelect(dPgetSysVal('TypeList'), 'type', 'id="idItemType" onchange="doChangeType(this)"', @$row->type)?>
			</td>
		</tr>
		<tr>
			<td align="right"> * <?=$AppUI->_('Code')?></td>
			<td >
				<input ftype=""  type="text" class="text" name="code" id="code"
				value="<?=@$row->code?>" maxlength="50" style="width: 98%" <?=$view?>/>

			</td>
		</tr>
		<tr>
			<td align="right"> * <?=$AppUI->_('Name')?></td>
			<td >
				<input type="text" class="text" name="name" id="name"
					value="<?=@$row->name?>" maxlength="50" style="width: 98%" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Active')?></td>
			<td>
				<input type="checkbox" name="active" id="active"
					value="1" <?=$view?> <?= (@$row->active == 1) ? "checked='true'":""?>/>
			</td>
		</tr>
		<tr>
			<td colspan="2" width="100%" align="left" style="padding-top: 0px">
			<div id="idItemScreen"  style="display: none">
			<table border="0" cellspacing="0" cellpadding="0" width="100%">
				<tr class="infotab">
					<td class="here">Screens</td>
				</tr>
			</table>
			<div style="border: 1px solid #AAA; border-top: 0px; width: 380px; height: 150px; padding: 4px;">
				<table id="idRequired"  border="0" cellpadding="2" cellspacing="2" width="100%">
					<tr>
						<td width="20px">
							<input type="checkbox" class="text" name="item_screens[]"
								value="ReferenceLine" <?=$view?> <?=in_array('ReferenceLine', $itemScreens) ? "checked='true'" : ""?>/>
						</td>
						<td nowrap="nowrap"><?=$AppUI->_('Reference line')?></td>
						<td width="20px">
							<input type="checkbox" class="text" name="item_screens[]"
								value="TaskItem" <?=$view?> <?=in_array('TaskItem', $itemScreens) ? "checked='true'" : ""?>/>
						</td>
						<td nowrap="nowrap"><?=$AppUI->_('Task item')?></td>
					</tr>
					<tr>
						<td width="20px">
							<input type="checkbox" class="text" name="item_screens[]"
								value="QuotationLine" <?=$view?> <?=in_array('QuotationLine', $itemScreens) ? "checked='true'" : ""?>/>
						</td>
						<td nowrap="nowrap"><?=$AppUI->_('Quotation line')?></td>
						<td width="20px">
							<input type="checkbox" class="text" name="item_screens[]"
								value="TaskLogItem" <?=$view?> <?=in_array('TaskLogItem', $itemScreens) ? "checked='true'" : ""?>/>
						</td>
						<td nowrap="nowrap"><?=$AppUI->_('Task log item')?></td>
					</tr>
				</table>
			</div>
		</td>
		</tr>
		</table>
			</div>
	</td>
</tr>
<tr>
	<td align="center" colspan="2" style="border-top: 1px solid #E0E0E0">
	<? if (!$view) { ?>
		<input type="submit" class="button" value="<?=$AppUI->_('Save')?>"/>
		<input type="button" class="button" value="<?=$AppUI->_('Cancel')?>" onClick="doCloseType()"/>
	<? } else { ?>
		<input type="button" class="button" value="<?=$AppUI->_('Close')?>" onClick="doCloseType()"/>
	<? } ?>
	</td>
</tr>
</table>
</form>
<div id="validation_rules" style="visibility: hidden; display: none">
	var fTypeCode = new LiveValidation('code');
	fTypeCode.add( Validate.Presence);
	fTypeCode.add( Validate.Exclusion , {within: <?=$qCL?>});

	var fTypeName = new LiveValidation('name');
	fTypeName.add( Validate.Presence);

	doChangeType($('idItemType'));

	new Tips($$('.text'));
</div>